const inquirer = require('inquirer')
const Crawler = require('./crawler')
const { NODE_ENV = 'production' } = process.env;

class Main extends Crawler {
  douyinURL = null
  constructor(NODE_ENV) {
    super(NODE_ENV)
  }
  async start() {
    try {
      const result = await inquirer.prompt([{
        type: 'input',          // 类型
        name: 'douyinURL',       // 字段名称，在then里可以打印出来
        message: `输入要爬取的抖音用户分享链接：`   // 提示信息
      }])
      let url = result.douyinURL.trim()
      if (!url) throw new Error('链接不能为空！')
      try {
        const reg = /(http:\/\/|https:\/\/)((\w|=|\?|\.|\/|&|-)+)/g;
        this.douyinURL = url.match(reg)[0]
      } catch (err) {
        console.error(err)
        throw new Error(`链接输入错误(${url})`)
      }
      this.openBrowser(this.douyinURL)
    } catch (err) {
      throw err
    }
  }
}

(async function main() {
  const main = new Main(NODE_ENV)
  // 初始化
  await main.init()
  // 运行
  await main.start()
})()